package com.ruoyi.financial.service.impl;

import java.util.List;
import java.util.ArrayList;
import com.ruoyi.common.core.domain.Ztree;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.financial.mapper.FThingMapper;
import com.ruoyi.financial.domain.FThing;
import com.ruoyi.financial.service.IFThingService;
import com.ruoyi.common.core.text.Convert;

/**
 * 物品管理Service业务层处理
 * 
 * @author xunzhe
 * @date 2024-01-23
 */
@Service
public class FThingServiceImpl implements IFThingService 
{
    @Autowired
    private FThingMapper fThingMapper;

    /**
     * 查询物品管理
     * 
     * @param thingId 物品管理主键
     * @return 物品管理
     */
    @Override
    public FThing selectFThingByThingId(Long thingId)
    {
        return fThingMapper.selectFThingByThingId(thingId);
    }

    /**
     * 查询物品管理列表
     * 
     * @param fThing 物品管理
     * @return 物品管理
     */
    @Override
    public List<FThing> selectFThingList(FThing fThing)
    {
        return fThingMapper.selectFThingList(fThing);
    }

    /**
     * 新增物品管理
     * 
     * @param fThing 物品管理
     * @return 结果
     */
    @Override
    public int insertFThing(FThing fThing)
    {
        fThing.setCreateTime(DateUtils.getNowDate());
        if(fThing.getParentId() != null){
            FThing info = fThingMapper.selectFThingByThingId(fThing.getParentId());
            if(info != null){
                fThing.setAncestors(info.getAncestors() + "," + fThing.getParentId());
            }
        }

        return fThingMapper.insertFThing(fThing);
    }

    /**
     * 修改物品管理
     * 
     * @param fThing 物品管理
     * @return 结果
     */
    @Override
    public int updateFThing(FThing fThing)
    {
        fThing.setUpdateTime(DateUtils.getNowDate());
        if(fThing.getParentId() != null){
            FThing info = fThingMapper.selectFThingByThingId(fThing.getParentId());
            if(info != null){
                fThing.setAncestors(info.getAncestors() + "," + fThing.getParentId());
            }
        }
        return fThingMapper.updateFThing(fThing);
    }

    /**
     * 批量删除物品管理
     * 
     * @param thingIds 需要删除的物品管理主键
     * @return 结果
     */
    @Override
    public int deleteFThingByThingIds(String thingIds)
    {
        return fThingMapper.deleteFThingByThingIds(Convert.toStrArray(thingIds));
    }

    /**
     * 删除物品管理信息
     * 
     * @param thingId 物品管理主键
     * @return 结果
     */
    @Override
    public int deleteFThingByThingId(Long thingId)
    {
        return fThingMapper.deleteFThingByThingId(thingId);
    }

    /**
     * 查询物品管理树列表
     * 
     * @return 所有物品管理信息
     */
    @Override
    public List<Ztree> selectFThingTree()
    {
        List<FThing> fThingList = fThingMapper.selectFThingList(new FThing());
        List<Ztree> ztrees = new ArrayList<Ztree>();
        for (FThing fThing : fThingList)
        {
            Ztree ztree = new Ztree();
            ztree.setId(fThing.getThingId());
            ztree.setpId(fThing.getParentId());
            ztree.setName(fThing.getThingName());
            ztree.setTitle(fThing.getThingName()+"--"+fThing.getThingUnit());
            ztrees.add(ztree);
        }
        return ztrees;
    }

    public int checkCount(Long thingId){
        return fThingMapper.checkCount(thingId);
    }

    public List<FThing> checkName(String thingName){
        return fThingMapper.checkName(thingName);
    }

    public List<Ztree> selectFThingCategoryTree(){
        //List<FThing> fThingList = fThingMapper.selectFThingCategory();
        List<FThing> fThingList = fThingMapper.selectFThingList(new FThing());
        List<Ztree> ztrees = new ArrayList<Ztree>();
        for (FThing fThing : fThingList)
        {
            Ztree ztree = new Ztree();
            ztree.setId(fThing.getThingId());
            ztree.setpId(fThing.getParentId());
            ztree.setName(fThing.getThingName());
            ztree.setTitle(fThing.getThingName());
            ztrees.add(ztree);
        }
        return ztrees;
    }


}
